#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"
void StackPush(Stack* ps, DataType x)
{
	assert(ps);
	if (ps->Top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* tmp = (DataType*)realloc(ps->data, newcapacity*sizeof(DataType));
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		else
		{
			ps->data = tmp;
			ps->capacity = newcapacity;
		}
	}
	ps->data[ps->Top] = x;
	ps->Top++;
}
void StackInit(Stack* ps)
{
	ps->capacity = 0;
	ps->data = NULL;
	ps->Top = 0;
}
bool StackIsEmpty(Stack* ps)
{
	assert(ps);
	return ps->Top == 0;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackIsEmpty(ps));
	ps->Top--;
}
void StackPrint(Stack* ps)
{
	assert(ps);
	assert(!StackIsEmpty(ps));
	for (int i = 0;i < ps->Top;i++)
	{
		printf("%d ", ps->data[i]);
	}
	printf("\n");
}
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = 0;
	ps->Top = 0;
}
int StackTop(Stack* ps)
{
	assert(ps);
	return ps->Top;
}



